package com.jeecg.demo.util;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.jeecgframework.core.util.ContextHolderUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.system.manager.ClientManager;
import org.jeecgframework.web.system.pojo.base.Client;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GetUserTenantId {

    private SystemService systemService;

    @Autowired
    public void setSystemService(SystemService systemService) {
        this.systemService = systemService;
    }

    public void main() {
        System.out.println("main");
        String returnValue = getSessionUser().getCurrentDepart().getOrgCode();

    }

    public String getOrgcode() {
        String returnValue = getSessionUser().getCurrentDepart().getOrgCode();
        return returnValue;
    }

    public String getSql() {
        String sql = "select orgid,worktypeid from tenantid_user  where orgid=?";
        return sql;
    }

    public int getTenantid() {
        String returnValue = getSessionUser().getCurrentDepart().getOrgCode();
        String ret = "";
        System.out.println("getuserid");
        String sql = "select orgid,worktypeid from tenantid_user  where orgid=?";
        // Map<String, Object> ccad= systemService.findOneForJdbc(sql,
        // returnValue);
        // Map<String, Object> userOrgMap =
        // systemService.findOneForJdbc("select org_id as orgId from t_s_user_org where user_id=?",
        // "8a80cb81696a74be01696a760bb40001");
        // System.out.println(userOrgMap);
        try {
            System.out.println(sql + "---" + returnValue);
            // Map<String, Object> data= systemService.findOneForJdbc(sql,
            // returnValue);

            Integer data2 = systemService
                    .executeSql(
                            "select orgid,worktypeid from tenantid_user  where orgid=?",
                            returnValue);
            System.out.println(data2);
            // if (!data.isEmpty()) {
            // JSONObject jo = JSONObject.fromObject(data);
            // ret = jo.toString();
            // System.out.println("test---"+ret+"----"+ret);
            // }

        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (ret != "") {
            System.out.println("null" + ret);
        }
        else {
            System.out.println("9" + ret);
            int tempnum = 9;
        }
        // int typeidnumber=Integer.valueOf(ret).intValue();
        // System.out.println("----number"+typeidnumber);
        return 9;
    }

    // test
    public static final TSUser getSessionUser() {
        ClientManager clientManager = ClientManager.getInstance();
        HttpSession session = ContextHolderUtils.getSession();
        if (clientManager.getClient(session.getId()) != null) {

            return clientManager.getClient(session.getId()).getUser();

        }
        else {
            TSUser u = (TSUser) session
                    .getAttribute(ResourceUtil.LOCAL_CLINET_USER);
            Client client = new Client();
            client.setIp("");
            client.setLogindatetime(new Date());
            client.setUser(u);
            clientManager.addClinet(session.getId(), client);
        }

        return null;
    }

}
